<?php 

namespace pmvc\io;

class ResourceStream
	implements Stream {

	private $resource;

	public function __construct($resource) {
		$this->resource = $resource;
	}

	public function isEof() {
		return feof($this->resource);
	}

	public function isOpen() {
		return is_resource($this->resource);
	}

	public function isClosed() {
		return !$this->isOpen();
	}

	public function close() {
		return fclose($this->resource);
	}

	public function flush() {
		return fflush($this->resource);
	}

	public function truncate($size) {
		ftruncate($this->resource, $size);
		$this->seek(0);
	}

	public function read($length) {
		return fread($this->resource, $length);
	}
	
	public function write($data, $length=-1) {
		return ($length!=-1)
			? fwrite($this->resource, $data, $length)
			: fwrite($this->resource, $data);
	}

	public function forward($length) {
		return fseek($this->resource, $length, SEEK_CUR)==0;
	}

	public function rewind($length) {
		return fseek($this->resource, -$length, SEEK_CUR)==0;
	}

	public function seek($location) {
		return fseek($this->resource, $location, SEEK_SET)==0;
	}

	public function begin() {
		return rewind($this->resource);
	}

	public function end() {
		return fseek($this->resource, 0, SEEK_END)==0;
	}

	public function position() {
		return ftell($this->resource);
	}

	public function getResource() {
		return $this->resource;
	}

	public function passThru() {
		return fpassthru($this->resource);
	}

	public function readAll() {
		return stream_get_contents($this->resource);
	}

}

?>